package crud

import (
	"fmt"

	"gorm.io/gorm"
)

func UseSoftDelete(db *gorm.DB) {
	// #region soft-delete
	type User struct {
		ID        uint `gorm:"primarykey"`
		Name      string
		Age       int8
		DeletedAt gorm.DeletedAt `gorm:"index"`
	}

	var user = User{ID: 10}
	db.First(&user)
	// #endregion soft-delete
}

func QueryMain() {
	// #region basic
	type User struct {
		ID   uint `gorm:"primarykey"`
		Name string
		Age  int8
	}

	db, err := CreateDB()

	user := User{}
	// #endregion basic

	if err == nil {
		// #region first
		db.First(&user)
		// #endregion first

		// #region take
		db.Take(&user)
		// #endregion take

		// #region last
		db.Last(&user)
		// #endregion last

		user = User{}
		// #region whereId
		db.First(&user, 10)
		// #endregion whereId

		// #region whereIn
		users := []User{}
		db.Find(&users, []int{1, 2, 3})
		// #endregion whereIn

		user = User{}
		// #region prepare
		db.First(&user, "id = ?", "10")
		// #endregion prepare

		// #region model
		user = User{ID: 10}
		db.First(&user)

		user = User{ID: 10}
		db.Model(User{ID: 10}).First(&user)
		// #endregion model

		UseSoftDelete(db)

		// #region find-all
		users = []User{}
		result := db.Find(&users)
		// #endregion find-all

		fmt.Println(result)
	}
}
